﻿#include "FITKAbaqusHDF5AdaptorCoordinateSystem.h"
#include "FITK_Kernel/FITKCore/FITKEnumTransformer.hpp"
#include "FITK_Interface/FITKInterfaceIO/FITKAbstractHDF5Reader.h"
#include "FITK_Interface/FITKInterfaceIO/FITKAbstractHDF5Writer.h"
#include "FITK_Interface/FITKInterfaceModel/FITKCoordinateSystem.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaTransform.h"

namespace IO
{
    QString FITKAbaqusHDF5AdaptorCoordinateSystem::getAdaptorClass()
    {
        return "FITKAbaqusHDF5AdaptorCoordinateSystem";
    }

    bool FITKAbaqusHDF5AdaptorCoordinateSystem::adaptR()
    {
        auto coor = dynamic_cast<Interface::FITKCoordinateSystem*>(_dataObj);
        if (!_reader || !coor || !_h5Group) return false;
        bool isR = true;
        isR &= readCoordinateSystem(coor, *_h5Group);
        return isR;
    }

    bool FITKAbaqusHDF5AdaptorCoordinateSystem::adaptW()
    {
        auto coor = dynamic_cast<Interface::FITKCoordinateSystem*>(_dataObj);
        if (!_writer || !coor || !_h5Group) return false;
        bool isW = true;
        isW &= writeCoordinateSystem(coor, *_h5Group);
        return isW;
    }

    bool FITKAbaqusHDF5AdaptorCoordinateSystem::readCoordinateSystem(Interface::FITKCoordinateSystem * coor, H5::Group & h5Group)
    {
        Interface::FITKAbaTransform* transform = dynamic_cast<Interface::FITKAbaTransform*>(coor);
        if (!transform)return false;
        bool isR = true;
        isR &= readNDataObject(transform, h5Group);
        QList<double> origin;
        if (readDoubleAttrbuteDataSet(h5Group, "Origin", origin, 1, 3))
            transform->setOrigin(origin[0], origin[1], origin[2]);

        QList<double> p1, p2;
        readDoubleAttrbuteDataSet(h5Group, "P1", p1, 1, 3);
        readDoubleAttrbuteDataSet(h5Group, "P2", p2, 1, 3);
        double dp1[3], dp2[3];
        if (p1.size() == 3)
        {
            dp1[0] = p1[0];
            dp1[1] = p1[1];
            dp1[2] = p1[2];
        }

        if (p2.size() == 3)
        {
            dp2[0] = p2[0];
            dp2[1] = p2[1];
            dp2[2] = p2[2];
        }

        transform->setTransform(dp1, dp2);

        return isR;
    }

    bool FITKAbaqusHDF5AdaptorCoordinateSystem::writeCoordinateSystem(Interface::FITKCoordinateSystem * coor, H5::Group & h5Group)
    {
        Interface::FITKAbaTransform* transform = dynamic_cast<Interface::FITKAbaTransform*>(coor);
        if (!transform)return false;
        bool isValid = false;

        std::string groupName = createParentAttribute(transform, h5Group);
        if (groupName.empty())return false;
        auto coordinateSystemGroup = h5Group.createGroup(groupName);

        auto eSysType = transform->getSysType();
        //枚举转换字符
        Core::FITKEnumTransfer<Interface::FITKCoordinateSystem::FCSysType> fitkTypeTrafer;
        auto sSysType = fitkTypeTrafer.toStrting(eSysType, isValid);
        if (!isValid) return false;
        writeStrAttribute(coordinateSystemGroup, "SysType", sSysType.toStdString());

        double origin[3];
        transform->getOrigin(origin);
        writeDoubleAttribute(coordinateSystemGroup, "Origin", &origin, 1, 3);

        double p1[3], p2[3];
        transform->getTransform(p1, p2);
        writeDoubleAttribute(coordinateSystemGroup, "P1", &p1, 1, 3);
        writeDoubleAttribute(coordinateSystemGroup, "P2", &p2, 1, 3);
        
        return writeNDataObject(transform, coordinateSystemGroup);
    }

}


